﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;


namespace id_watermark
{
    internal class Program
    {

        static void AddWatermark(string path, string save, string text, int alpha, int width, int height, int fontSize)
        {
            if (!File.Exists(path))
            {
                throw new FileNotFoundException();
            }
            var baseImage = Image.FromFile(path);
            var graphics = Graphics.FromImage(baseImage);
            var font = new Font("Microsoft YaHei UI", fontSize);
            Brush brush = new SolidBrush(Color.FromArgb(alpha, Color.Black));
            var size = graphics.MeasureString(text, font);

            var matrix = graphics.Transform;
            matrix.Rotate(-15);
            //matrix.RotateAt(-10, new PointF(0, 0));
            graphics.Transform = matrix;
            for (var i = -baseImage.Width; i < baseImage.Width * 2; i += (int)size.Width + width)
            {
                for (var j = -baseImage.Height; j < baseImage.Height * 2; j += height)
                {
                    graphics.DrawString(text, font, brush, new PointF(i, j));
                }
            }
            MemoryStream memoryStream = new MemoryStream();
            baseImage.Save(memoryStream, ImageFormat.Png);
            var bytes = memoryStream.ToArray();
            File.WriteAllBytes(save, bytes);

            Console.WriteLine("success");
        }

        static void DisplayHelp()
        {
            Console.WriteLine("-help: 显示帮助消息");
            Console.WriteLine("-i <inputFilePath>: 指定输入文件路径");
            Console.WriteLine("-o <outputFilePath>: 指定输出文件路径");
            Console.WriteLine("-t <textName>: 水印文本");
            Console.WriteLine("-a <alpha>: 水印透明度(0-255),默认 70");
            Console.WriteLine("-w <width>: 水印左右间隔,默认 10");
            Console.WriteLine("-h <height>:水印上下间隔,默认 50");
            Console.WriteLine("-s <fontSize>:水印字体大小,默认 20");
            Console.WriteLine("示例: id-watermark.exe -i intpu.png -o out.png -t 仅供\\\"某某公司\\\"实名认证使用 -a 70 -w 10 -h 50 -s 20");
        }

        static Dictionary<string, string> ParseArguments(string[] args)
        {
            Dictionary<string, string> parsedArgs = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

            for (int i = 0; i < args.Length; i++)
            {
                if (args[i].StartsWith("-"))
                {
                    string key = args[i].ToLowerInvariant();
                    string value = null;

                    if (i + 1 < args.Length && !args[i + 1].StartsWith("-"))
                    {
                        value = args[i + 1];
                        i++;
                    }

                    parsedArgs[key] = value;
                }
            }

            return parsedArgs;
        }


        static void Main(string[] args)
        {
            Dictionary<string, string> parsedArgs = ParseArguments(args);
            if (parsedArgs == null || parsedArgs.Count < 3 || parsedArgs.ContainsKey("-help"))
            {
                DisplayHelp();
                return;
            }

            string inputFilePath = parsedArgs.ContainsKey("-i") ? parsedArgs["-i"] : null;
            string outputFilePath = parsedArgs.ContainsKey("-o") ? parsedArgs["-o"] : null;
            string text = parsedArgs.ContainsKey("-t") ? parsedArgs["-t"] : null;
            int alpha = parsedArgs.ContainsKey("-a") ? Int32.Parse(parsedArgs["-a"]) : 70;
            int width = parsedArgs.ContainsKey("-w") ? Int32.Parse(parsedArgs["-w"]) : 10;
            int height = parsedArgs.ContainsKey("-h") ? Int32.Parse(parsedArgs["-h"]) : 50;
            int fontSize = parsedArgs.ContainsKey("-s") ? Int32.Parse(parsedArgs["-s"]) : 20;

            Console.WriteLine("解析结果：");
            Console.WriteLine($"输入文件路径：{inputFilePath}");
            Console.WriteLine($"输出文件路径：{outputFilePath}");
            Console.WriteLine($"水印内容：{text}");
            Console.WriteLine($"水印透明度：{alpha}");
            Console.WriteLine($"水印左右间隔：{width}");
            Console.WriteLine($"水印上下间隔：{height}");
            Console.WriteLine($"水印字体大小：{fontSize}");

            AddWatermark(inputFilePath, outputFilePath, text, alpha, width, height, fontSize);
        }
    }
}
